package com.ljx.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljx.common.ResponseResult;
import com.ljx.domain.entity.Article;
import com.ljx.domain.entity.Menu;
import com.ljx.domain.vo.MenuTreeVo;
import com.ljx.domain.vo.RoleMenuTreeSelectVo;
import com.ljx.mapper.MenuMapper;
import com.ljx.service.MenuService;
import com.ljx.utils.SecurityUtils;
import com.ljx.utils.SystemConstants;
import com.ljx.utils.SystemConverter;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜单权限表(Menu)表服务实现类
 *
 * @author makejava
 * @since 2023-04-21 11:02:47
 */
@Service("menuService")
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Override
    public List<String> selectRoleKeyByUserId(Long id) {
        //如果是管理员查询拥有全部权限
        if(id == 1L){
            LambdaQueryWrapper<Menu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.in(Menu::getMenuType, SystemConstants.MENU_C,SystemConstants.MENU_F);
            lambdaQueryWrapper.eq(Menu::getStatus,SystemConstants.STATUS_NORMAL);
            List<Menu> list = list(lambdaQueryWrapper);
            List<String> perms = list.stream().map(p -> p.getPerms()).collect(Collectors.toList());
            return perms;
        }
        return getBaseMapper().selectPermsByUserId(id);
    }

    @Override
    public List<Menu> selectRouterMenuTreeByUserId(Long userId) {
        MenuMapper menuMapper = getBaseMapper();
        List<Menu> menus = null;
        //判断是否是管理员
        if(SecurityUtils.isAdmin()){
            //如果是 获取所有符合要求的Menu
            menus = menuMapper.selectAllRouterMenu();
        }else{
            //否则  获取当前用户所具有的Menu
            menus = menuMapper.selectRouterMenuTreeByUserId(userId);
        }

        //构建tree
        //先找出第一层的菜单  然后去找他们的子菜单设置到children属性中
        List<Menu> menuTree = builderMenuTree(menus,0L);
        return menuTree;
    }

    /**
     * 后台页面查询列表
     * @param menuName
     * @return
     */
    @Override
    public ResponseResult listAll(String menuName,String status) {
        LambdaQueryWrapper<Menu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.hasText(menuName),Menu::getMenuName,menuName);
        //正式发布的文章
        lambdaQueryWrapper.eq(StringUtils.hasText(status),Menu::getStatus,status);
        List<Menu> list = list(lambdaQueryWrapper);
        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult roleMenuTreeSelect(Long id) {
        List<Menu> menus = list();
        //以树形结构将Menus查询出来
        List<MenuTreeVo> options =  SystemConverter.buildMenuSelectTree(menus);
        //判断是否为超级管理员
        if(id==1L){
            LambdaQueryWrapper<Menu> queryWrapper=new LambdaQueryWrapper<>();
            //条件查询只查询id
            queryWrapper.select(Menu::getId);
            List<Menu> list =list(queryWrapper);
            List<Long> collect = list.stream().map(e -> e.getId()).collect(Collectors.toList());
            RoleMenuTreeSelectVo vo = new RoleMenuTreeSelectVo(collect,options);
            return ResponseResult.okResult(vo);
        }
        List<Long> longs = getBaseMapper().selectMenuListByRoleId(id);
        RoleMenuTreeSelectVo vo = new RoleMenuTreeSelectVo(longs,options);
        return ResponseResult.okResult(vo);
    }

    private List<Menu> builderMenuTree(List<Menu> menus, Long parentId) {
        List<Menu> menuTree = menus.stream()
                .filter(menu -> menu.getParentId().equals(parentId))
                .map(menu -> menu.setChildren(getChildren(menu, menus)))
                .collect(Collectors.toList());
        return menuTree;
    }

    /**
     * 获取存入参数的 子Menu集合
     * @param menu
     * @param menus
     * @return
     */
    private List<Menu> getChildren(Menu menu, List<Menu> menus) {
        List<Menu> childrenList = menus.stream()
                .filter(m -> m.getParentId().equals(menu.getId()))
                .map(m->m.setChildren(getChildren(m,menus)))
                .collect(Collectors.toList());
        return childrenList;
    }
}
